﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using System.Data;

namespace School.UserCentents
{
    public partial class AddMember : UserCheckPower
    {
        #region 传递参数
        /// <summary>
        /// ID
        /// </summary>
        public int ID
        {
            get
            {
                if (Request.QueryString["id"] == null || Request.QueryString["id"] == "")
                {
                    return 0;
                }
                return int.Parse(Request.QueryString["id"].ToString());
            }
        }
        #endregion
        BLL.Users Users = new BLL.Users();
        BLL.School Sch = new BLL.School();
        BLL.Classes Classes = new BLL.Classes();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (new BLL.Users().GetModel(LoginUserId).UserTypeID <= 3)
            {
                Panel p1 = (Panel)Master.FindControl("Panel1");
                Panel p2 = (Panel)Master.FindControl("Panel2");
                Panel p3 = (Panel)Master.FindControl("Panel3");
                Panel p4 = (Panel)Master.FindControl("Panel4");
                p1.CssClass = "sldmenuoff";
                p2.CssClass = "sldmenuon0";
                p3.CssClass = "sld1";
                p4.CssClass = "sld0";
                p3.Attributes.Add("style", "display: none");
                p4.Attributes.Add("style", "");
            }
            if (!IsPostBack)
            {
                PageLoad();
            }
        }

        private void PageLoad()
        {
            if (Users.GetModel(LoginUserId).UserTypeID == 1)
            {
                RadioButtonList1.DataSource = new BLL.UserType().GetList("ID not in (1,2)");
                RadioButtonList1.DataValueField = "ID";
                RadioButtonList1.DataTextField = "TypeName";
                RadioButtonList1.DataBind();

                XueXiao.Visible = true;
                DropDownList1.DataSource = Sch.GetAllList();
                DropDownList1.DataValueField = "ID";
                DropDownList1.DataTextField = "Name";
                DropDownList1.DataBind();
                DropDownList1.Items.Insert(0, new ListItem("请选择", "0"));
            }
            else if (Users.GetModel(LoginUserId).UserTypeID == 2)
            {
                RadioButtonList1.DataSource = new BLL.UserType().GetList("ID not in (1,2)");
                RadioButtonList1.DataValueField = "ID";
                RadioButtonList1.DataTextField = "TypeName";
                RadioButtonList1.DataBind();

                XueXiao.Visible = true;
                DropDownList1.DataSource = Sch.GetList("CreateUserID=" + LoginUserId);
                DropDownList1.DataValueField = "ID";
                DropDownList1.DataTextField = "Name";
                DropDownList1.DataBind();
                DropDownList1.Items.Insert(0, new ListItem("请选择", "0"));
            }
            else if (Users.GetModel(LoginUserId).UserTypeID == 3)
            {
                RadioButtonList1.DataSource = new BLL.UserType().GetList("ID not in (1,2,3)");
                RadioButtonList1.DataValueField = "ID";
                RadioButtonList1.DataTextField = "TypeName";
                RadioButtonList1.DataBind();
                DropDownList1.Items.Insert(0, new ListItem("请选择", "0"));
            }
            else
            {
                RadioButtonList1.DataSource = new BLL.UserType().GetList("ID in (5,6)");
                RadioButtonList1.DataValueField = "ID";
                RadioButtonList1.DataTextField = "TypeName";
                RadioButtonList1.DataBind();
            }
            if (ID > 0)
            {
                Model.Users u = Users.GetModel(ID);
                if (u != null)
                {
                    RadioButtonList1.SelectedValue = u.UserTypeID.ToString();
                    DropDownList1.SelectedValue = u.SchoolID.ToString();
                    txtLoginName.Text = u.MembeLoginName;
                    txtName.Text = u.MemberName;
                    txtPhone.Text = u.Phone;
                    RadioButtonList2.SelectedValue = u.Sex.ToString();
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (RadioButtonList1.SelectedValue == "")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "a", "<script>alert('请选择用户组！');</script>", false);
                return;
            }
            if (XueXiao.Visible == true)
            {
                if (DropDownList1.SelectedValue == "0")
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "a", "<script>alert('请选择校区！');</script>", false);
                    return;
                }
            }
            DataSet ds = Users.GetAllList();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (txtLoginName.Text == ds.Tables[0].Rows[i]["MembeLoginName"].ToString())
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "a", "<script>alert('用户名重复，请重新输入！');</script>", false);
                    return;
                }
            }
            if (ID > 0)
            {
                Model.Users u = Users.GetModel(ID);
                u.UserTypeID = int.Parse(RadioButtonList1.SelectedValue);
                u.MembeLoginName = txtLoginName.Text;
                u.MemberName = txtName.Text;
                u.Phone = txtPhone.Text;
                if (XueXiao.Visible == true)
                {
                    u.SchoolID = int.Parse(DropDownList1.SelectedValue);
                }
                if (!string.IsNullOrEmpty(txtPWD.Text))
                {
                    u.MembePassWord = Encryption.GetMD5(txtPWD.Text);
                }
                Users.Update(u);
                ScriptHelper.AlertRedirect("修改成功！", "AddMember.aspx?id=" + ID);
            }
            else
            {
                if (string.IsNullOrEmpty(txtPWD.Text))
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "a", "<script>alert('密码不能为空！');</script>", false);
                    return;
                }
                Model.Users u = new Model.Users();
                u.MembeLoginName = txtLoginName.Text;
                u.MemberName = txtName.Text;
                u.Phone = txtPhone.Text;
                u.MembePassWord = Encryption.GetMD5(txtPWD.Text);
                u.UserTypeID = int.Parse(RadioButtonList1.SelectedValue);
                u.Sex = int.Parse(RadioButtonList2.SelectedValue);
                u.Pic = "1";
                if (XueXiao.Visible == true)
                {
                    u.SchoolID = int.Parse(DropDownList1.SelectedValue);
                    u.Remark = LoginUserId.ToString();
                }
                else
                {
                    u.SchoolID = Users.GetModel(LoginUserId).SchoolID;
                    u.Remark = Users.GetModel(LoginUserId).Remark;
                }
                if (Users.GetModel(LoginUserId).UserTypeID == 3)
                {
                    u.Remark2 = LoginUserId;
                }
                else if (Users.GetModel(LoginUserId).UserTypeID == 4)
                {
                    u.Remark2 = Users.GetModel(LoginUserId).Remark2;
                }

                u.State = 1;
                u.CreateTime = DateTime.Now;
                u.CreateUserID = LoginUserId;
                Users.Add(u);
                ScriptHelper.AlertRedirect("增加成功！", "Member.aspx");
            }
        }
    }
}